package com.homelock.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.alibaba.druid.util.StringUtils;

public class DateUtils {
	
	public static final String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	
	public static final String ISO_DATE_FORMAT = "yyyy-MM-dd";
	
	public static final String DATE_PATTERN_001="yyMMddHHmmss";

	 /**
     * 按照指定格式把时间转换成字符串，格式的写法类似yyyy-MM-dd HH:mm:ss.SSS
     * 
     * @param date
     *            时间
     * @param pattern
     *            格式
     * @return 时间字符串
     */
    public static String date2String(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        return (new SimpleDateFormat(pattern)).format(date);
    }
    
    
    public static Date string2Date(String str, String pattern) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = dateFormat.parse(str);
        }
        catch (ParseException e) {
           return null;
        }
        return date;
    }
    
    public static Date dateAddTime(Date date,int type,int addTime){
    	Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(type, addTime);
		return cal.getTime();
    }
    
    public static int subDay(Date startDate,Date endDate){
    	Calendar startCalendar = Calendar.getInstance();
    	startCalendar.setTime(startDate);
        int startDay = startCalendar.get(Calendar.DAY_OF_YEAR);
        
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        int endDay = endCalendar.get(Calendar.DAY_OF_YEAR);
        
        return endDay-startDay;
    }
}
